package thread.bisuo;

import java.util.concurrent.CountDownLatch;
/**
 *
 *
 * */
public class TestHarness {


     public static  void main(String[] args) throws InterruptedException {



     long time = timeTask(3, new Runnable() {
             @Override
             public void run() {
                 System.out.println("0k");
             }
         });
     System.out.println(time);
     }

    public static long timeTask(int nThreads, final Runnable task) throws InterruptedException {
        final CountDownLatch startGate = new CountDownLatch(1);
        final CountDownLatch endGate = new CountDownLatch(nThreads);
        for(int i =0;i<nThreads;i++){
            Thread t = new Thread(){

                @Override
                public void run() {
                    super.run();
                    try {
                        startGate.await();
                        try{
                            task.run();

                        }finally {
                            endGate.countDown();
                        }
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
            };
             t.start();
        }

        long start = System.nanoTime();
        startGate.countDown();
        endGate.await();
        long end = System.nanoTime();
        return end - start;
    }
}
